package com.pk.platform.business.core.web;

import java.io.File;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.opensymphony.xwork2.ActionSupport;
import com.pk.platform.business.core.service.IListViewService;
import com.pk.platform.business.core.vo.ListViewVO;
import com.pk.platform.domain.privilege.User;
import com.pk.platform.util.constant.Constant;
import com.pk.platform.util.page.ListPage;
import com.pk.platform.util.page.Pager;

public class GenericAction extends ActionSupport
{
	protected static String FILE = "file";
	
	protected String message;
    protected String urlAddress;//操作成功后,指定要回到哪个页面
    protected Pager pager = new Pager();
    protected ListPage lp; //列表
    protected List<Map<String, Object>> list; //列表
    
    protected File attachment; //批量上传文件
    protected String attachmentContentType; //文件类型
    protected String attachmentFileName; //文件名
    
    protected String nextPage; //下一个页面
    protected String prePage; //上一个页面
    protected String returnAbled; //是否可返回
    
    protected String menuLink; //菜单导航
    protected String parentMethod; //父页面方法
    protected String isMd; //是否模式窗口
    protected String today; //今天日期
    protected String operateChn; //修改或查看
    protected String saveType; //更新或新增
    protected Map<String, Object> gridObj = new HashMap<String, Object>();
    protected Map<String, String> paramObj = new HashMap<String, String>();
    
    protected IListViewService listViewService;
    protected ListViewVO lvvo = new ListViewVO();
    protected String formatterScript;
    
    /**
	 * <description> 获得request
	 * @return
	 */
	protected HttpServletRequest getRequest()
	{
		return ServletActionContext.getRequest();
	}
	
	/**
	 * <description> 获得response
	 * @return
	 */
	protected HttpServletResponse getResponse()
	{
		return ServletActionContext.getResponse();
	}
	
	/**
	 * <description> 获得session
	 * @return
	 */
	protected HttpSession getSession()
	{
		return this.getRequest().getSession();
	}
	
	/**
	 * <description> 获得登录用户
	 * @return
	 */
	protected User getLoginUser()
	{
		return (User)this.getSession().getAttribute(Constant.USER);
	}
	
	/**
	 * <p>
	 * 说明:获得项目名称
	 * </p>
	 * @return
	 */
	protected String getContextPath()
	{
	    return this.getRequest().getContextPath();
	}
	
	/**
	 * <p>
	 * 说明:获得请求地址
	 * </p>
	 * @return
	 */
	protected String getServletPath()
	{
	    return this.getRequest().getServletPath();
	}
	
	/**
	 * <p>
	 * 说明:获得绝对路径
	 * </p>
	 * @return
	 */
	protected String getRealPath(String path)
	{
	    return this.getSession().getServletContext().getRealPath(path);
	}
	
    /**
     * <p>
     * 说明:获得返回路径
     * </p>
     * @return
     */
    protected String getReturnPath()
    {
        LinkedHashMap<String,String> urlMap = (LinkedHashMap<String,String>)this.getSession().getAttribute("urlMap");
        String url = this.getRequest().getContextPath() + "/";
        if(urlMap != null)
        {
            url += urlMap.get(this.prePage) + "&isBack=Y";
        }
        return url;
    }

    
    /**
     * <p>
     * 说明:获得当前页面路径
     * </p>
     * @return
     */
    protected String getCurrentPath()
    {
        String contextPath = this.getRequest().getContextPath();
        LinkedHashMap<String,String> urlMap = (LinkedHashMap<String,String>)this.getSession().getAttribute("urlMap");
        String url = "";
        if(urlMap != null)
        {
            url = contextPath + "/" + urlMap.get(this.nextPage) + "&isBack=Y";
        }
        return url;
    }
    
    //url如:inner/dept/addDept.action
    protected void sendRedirect(String url)
    {
    	try
    	{
    		this.getResponse().sendRedirect(this.getContextPath() + "/" + url);
    	}
    	catch (Exception e)
    	{
			e.printStackTrace();
		}
    }
    
    //下载
    protected void toDownload(String filePath, String name)
    {
    	this.sendRedirect("inner/core/download.action?absoluteFilePath=" + filePath + "&attachmentFileName=" + name);
    }
    
    //初始化列表
    protected void initGrid()
    {
    	gridObj = listViewService.initGrid(lvvo.getListCode());
    	formatterScript = (String)gridObj.get("formatterScript");
    }
	
    public String getMessage() {
        return message;
    }
    
    public void setMessage(String message) {
        this.message = message;
    }
    
    public Pager getPager() {
        return pager;
    }

    public void setPager(Pager pager) {
        this.pager = pager;
    }
    
    public String getUrlAddress() {
        return urlAddress;
    }
    
    public void setUrlAddress(String urlAddress) {
        this.urlAddress = urlAddress;
    }
    
    public File getAttachment() {
        return attachment;
    }
    
    public void setAttachment(File attachment) {
        this.attachment = attachment;
    }
    
    public String getAttachmentContentType() {
        return attachmentContentType;
    }

    public void setAttachmentContentType(String attachmentContentType) {
        this.attachmentContentType = attachmentContentType;
    }

    public String getAttachmentFileName() {
        return attachmentFileName;
    }
    
    public void setAttachmentFileName(String attachmentFileName) {
        this.attachmentFileName = attachmentFileName;
    }
    
    public String getNextPage() {
        return nextPage;
    }

    public void setNextPage(String nextPage) {
        this.nextPage = nextPage;
    }

    public String getPrePage() {
        return prePage;
    }
    
    public void setPrePage(String prePage) {
        this.prePage = prePage;
    }
    
    public String getReturnAbled() {
        return returnAbled;
    }
    
    public void setReturnAbled(String returnAbled) {
        this.returnAbled = returnAbled;
    }
    
    public String getMenuLink() {
        return menuLink;
    }

    public void setMenuLink(String menuLink) {
        this.menuLink = menuLink;
    }

	public ListPage getLp() {
		return lp;
	}

	public void setLp(ListPage lp) {
		this.lp = lp;
	}

	public static String getFILE() {
		return FILE;
	}

	public static void setFILE(String file) {
		FILE = file;
	}

	public String getParentMethod() {
		return parentMethod;
	}

	public void setParentMethod(String parentMethod) {
		this.parentMethod = parentMethod;
	}

	public String getIsMd() {
		return isMd;
	}

	public void setIsMd(String isMd) {
		this.isMd = isMd;
	}

	public String getToday() {
		return today;
	}

	public void setToday(String today) {
		this.today = today;
	}

	public String getOperateChn() {
		return operateChn;
	}

	public void setOperateChn(String operateChn) {
		this.operateChn = operateChn;
	}

	public List<Map<String, Object>> getList() {
		return list;
	}

	public void setList(List<Map<String, Object>> list) {
		this.list = list;
	}

	public String getSaveType() {
		return saveType;
	}

	public void setSaveType(String saveType) {
		this.saveType = saveType;
	}

	public Map<String, Object> getGridObj() {
		return gridObj;
	}

	public void setGridObj(Map<String, Object> gridObj) {
		this.gridObj = gridObj;
	}

	public Map<String, String> getParamObj() {
		return paramObj;
	}

	public void setParamObj(Map<String, String> paramObj) {
		this.paramObj = paramObj;
	}

	public IListViewService getListViewService() {
		return listViewService;
	}

	public void setListViewService(IListViewService listViewService) {
		this.listViewService = listViewService;
	}

	public ListViewVO getLvvo() {
		return lvvo;
	}

	public void setLvvo(ListViewVO lvvo) {
		this.lvvo = lvvo;
	}

	public String getFormatterScript() {
		return formatterScript;
	}

	public void setFormatterScript(String formatterScript) {
		this.formatterScript = formatterScript;
	}
	
}
